home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-02-27 | 3.0 KB | 141 lines | [TEXT/CWIE] |
- /* SK8 © 1997 Apple Computer, Inc.
- This code is protected under the current SK8 License
- See http://sk8.research.apple.com/ for more information
- Apple Research Laboratories
- */
-
-
- /*
- table
- a look-up list that adheres to the SK8 collection protocol
- */
-
-
- public class table extends list {
-
- private Object mKey;
-
- //constructor
- public table(){
- super();
- mKey = null;
- }
-
- //overriding newSibling
- protected list newSibling() {
- table tc = new table();
- return tc;
- }
-
- //swapValues
- protected void swapValues(list inOther) {
- super.swapValues(inOther);
-
- Object theObj = ((table)inOther).mKey;
- ((table)inOther).mKey = this.mKey;
- this.mKey = theObj;
- }
- /**
- ** collection protocol stuff
- ** inherited from List collection
- **/
-
-
- /**
- ** lookup table stuff
- **/
-
- // keyatvisitstate
- public Object keyatvisitstate(visitstate inState){
- checkvisitstatetype(inState);
- return ((table)((listvisitstate)inState).mCell).mKey;
- };
-
- //visitatateatkey
- public visitstate visitatateatkey(Object inKey) {
- table scanner = this;
- if (inKey == null) {
- while ((scanner.mRest != null) && scanner.mKey != null) {
- scanner = (table)(scanner.mRest);
- }
- if (scanner.mKey == null) {
- return newvisitstate(this, scanner);
- } else {
- return null;
- }
-
- } else {
- while ((scanner.mRest != null) && !(inKey.equals(scanner.mKey))) {
- scanner = (table)(scanner.mRest);
- }
- if (inKey.equals(scanner.mKey)) {
- return newvisitstate(this, scanner);
- } else {
- return null;
- }
- }
- }
-
- // setelementatkey
- public visitstate setelementatkey(Object inKey, Object inElement) {
-
- visitstate v = visitatateatkey(inKey);
-
- //if no existing entry, add a new one
- if (v == null){
- v = initialvisitstate();
- v = insertatvisitstate(v, inElement);
- } else {
- // checkVisitStateType(v); //don't need this check, eh?
- setelementatvisitstate(v, inElement);
- }
- //now we know that v is the correct visit state and the value has been
- //correclty set. Now we just set the key and get outta here.
- listvisitstate lvs = (listvisitstate)v;
- if (lvs.mCell instanceof table) {
- table tc = (table)(lvs.mCell);
- tc.mKey = inKey;
- } //else {
- //throw new collectionexception("damn it!");}
- return v;
- }
-
-
- // elementatkey
- public Object elementatkey(Object inKey) {
- visitstate v = visitatateatkey(inKey);
- if (v == null) {
- return null;
- } else {
- return elementatvisitstate(v);
- }
- }
-
- //helpers
- public Object nth (Object n) {
- return elementatkey(n);
- }
- public boolean setnth (Object inKey, Object inElement) {
- try {
- setelementatkey(inKey, inElement);
- return true;
- } catch (RuntimeException E) {
- return false;
- }
- }
-
-
-
- public list keys() {
- list keylist = new list();
- Object i;
- for (visitstate curstate = this.initialvisitstate(); (curstate != null); curstate = this.succeedingvisitstate(curstate)) {
- i = this.keyatvisitstate(curstate);
- keylist.pushend(i);
- }
- return keylist;
- }
-
-
-
- }